package cn.tedu.store.controller;

import cn.tedu.store.entity.Address;
import cn.tedu.store.service.IAddressService;
import cn.tedu.store.util.JsonResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController   //表示一个控制器类且所有请求结果响应正文
@RequestMapping("addresses")
public class AddressController extends BaseController {

	@Resource
    private IAddressService addressService;

	@RequestMapping("addnew")
	public JsonResult<Void>  addnew(
			Address address,
			HttpSession session
			) {
		Integer uid=Integer.valueOf(session.getAttribute("uid").toString());
		String username=session.getAttribute("username").toString();
		addressService.addnew(address, uid, username);
		return new  JsonResult<>(SUCCESS);
	}

	@GetMapping("/")
	public JsonResult<List<Address>> getByUid(HttpSession session){
		Integer uid=getUidFromSession(session);
		List<Address> data=addressService.getByUid(uid);

		return new JsonResult<>(SUCCESS,data);
	}

	@RequestMapping("{aid}/set_default")
	public JsonResult<Void>  setDefault(
			@PathVariable("aid") Integer aid,
			HttpSession session
			) {
		Integer uid=Integer.valueOf(session.getAttribute("uid").toString());
		String username=session.getAttribute("username").toString();
		addressService.setDefault(aid, uid, username);
		return new  JsonResult<>(SUCCESS);
	}

	@RequestMapping("{aid}/delete")
	public JsonResult<Void> delete(
			@PathVariable("aid") Integer aid,
			HttpSession session
			) {
		Integer uid=Integer.valueOf(session.getAttribute("uid").toString());
		String username=session.getAttribute("username").toString();
		addressService.delete(aid, uid, username);
		return new  JsonResult<>(SUCCESS);
	}

}
